package com.dst.controller;
import com.dst.eneity.Student;
import com.dst.service.StudentService;
import com.dst.service.impl.StudentServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

@Controller
public class selectAllController {
    StudentService studentService = new StudentServiceImpl();
    @RequestMapping("/selectAll")
    public String selectAll( String curpage, Model model){
        int curPage=1;
        if(curpage!=null){
            curPage=Integer.parseInt(curpage);
        }
        int pageSize=3;
        PageInfo<Student> pageInfo = studentService.selectAll(curPage, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "/showAll.jsp";
    }
//    @RequestMapping("/insert")
//    public String insert(@RequestParam("name") String name,@RequestParam("tel") Integer tel,@RequestParam("gender") String gender,@RequestParam("age") Integer age){
//        Student student = new Student(null,gender,age,name,tel);
//        studentService.insertService(student);
//        return "redirect:/selectAll.do";
//    }
    @RequestMapping("/insert")
    public String insert(Student student){
        studentService.insertService(student);
        return "redirect:/selectAll.do";
    }
    @RequestMapping("/selectById")
    public String selectById(String str,Model model){
        int id = Integer.parseInt(str);
        Student student = studentService.selectById(id);
        model.addAttribute("student",student);
        return "/update.jsp";
    }
    @RequestMapping("/update")
    public String update(Student student){
        System.out.println(student);
        studentService.update(student);
        return "redirect:/selectAll.do";
    }
    @RequestMapping("/delete")
    public String delete(int id){

        studentService.delete(id);
        return "redirect:/selectAll.do";
    }
    @RequestMapping("/batchDelete")
    public String batchDelete(int [] ids){
        studentService.batchdelete(ids);
        return "redirect:/selectAll.do";
    }
 @RequestMapping("upload")
    public String upload(String name,MultipartFile mf) throws IOException {
     System.out.println("name = " + name);
     String originalFilename = mf.getOriginalFilename();
     String s = UUID.randomUUID().toString();
     String f=s+originalFilename;
     File file = new File("D:/test/"+f);
     mf.transferTo(file);
        return "redirect:/upload.jsp";
 }
 @RequestMapping("download")
 public void download(String name, HttpServletResponse resp) throws IOException {
     File file = new File("D:/test/"+name);
     resp.setHeader("content-disposition","attachment;fileName=a.png");
     FileUtils.copyFile(file,resp.getOutputStream());

 }
}
